package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.HhrErpCoreClient;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.*;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterHhrErpService {

    private static final Logger logger = LoggerFactory.getLogger(RouterHhrErpService.class);

    @Autowired
    private HhrErpCoreClient hhrErpCoreClient;

    /**
     * 子路由服务
     * @param clientKey
     * @param clientSecret
     * @param data
     * {
     *     "cmd":"sms.send"
     * }
     * @return
     */
    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data)  throws Exception {

        // 配送单查询
        if(EntranceConstant.ROUTER_HHRERP_DELIVERY_QUERY.equals(cmd)){

            logger.info("执行配送单查询请求，参数: {}", data.toJSONString());

            // 获取配置信息
            String deptId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_HHRERP, EntranceConstant.SETTING_TYPE_HHRERP_DEPTID);
            if(deptId == null || "".equals(deptId)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            HhrErpStoreDeliveryOrderPageReqDTO param = JSONObject.parseObject(data.toString(), HhrErpStoreDeliveryOrderPageReqDTO.class);
            param.setDeptId(deptId);

            // 执行请求
            HhrErpCommonResult resultDTO;
            try{
                resultDTO = hhrErpCoreClient.getStoreDeliveryOrderPage(param);
            } catch (Exception e){
                logger.error("调用配送单查询异常", e);
                resultDTO = new HhrErpCommonResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("配送单查询请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 退配单查询
        if(EntranceConstant.ROUTER_HHRERP_RETURN_QUERY.equals(cmd)){

            logger.info("执行配退单查询请求，参数: {}", data.toJSONString());

            // 获取配置信息
            String deptId = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_HHRERP, EntranceConstant.SETTING_TYPE_HHRERP_DEPTID);
            if(deptId == null || "".equals(deptId)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            HHrErpStoreReturnOrderPageReqDTO param = JSONObject.parseObject(data.toString(), HHrErpStoreReturnOrderPageReqDTO.class);
            param.setDeptId(deptId);

            // 执行请求
            HhrErpCommonResult resultDTO;
            try{
                resultDTO = hhrErpCoreClient.getStoreReturnOrderPageOpen(param);
            } catch (Exception e){
                logger.error("调用配退单查询异常", e);
                resultDTO = new HhrErpCommonResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("配退单查询请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
